﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.UserControl;
using System.Reflection;
using ExpertMIS.Brokers;
using ExpertMIS.Entities;

namespace ExpertMIS.Columns
{
    public partial class BulletinList : System.Web.UI.Page
    {
        private BulletinCatalog _catalog = BulletinCatalog.SocailBulletin;
        private BulletinBroker _bulletinBroker = new BulletinBroker();

        private void RefreshBulletin()
        {
            switch (_catalog)
            {
                case BulletinCatalog.OrganizationBulletin:
                    {
                        repBulletinList.DataSource = _bulletinBroker.GetOrganizationBulletins(15);
                        repBulletinList.DataBind();
                    }
                    break;
                case BulletinCatalog.SocailBulletin:
                    {
                        repBulletinList.DataSource = _bulletinBroker.GetSocialBulletins(15);
                        repBulletinList.DataBind();
                    }
                    break;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RefreshBulletin();
        }

        protected override void OnPreInit(EventArgs e)
        {
            // TODO, 切换不同的master

            _catalog = (BulletinCatalog)Enum.Parse(typeof(BulletinCatalog), Request["catalog"]);

            switch (_catalog)
            {
                case BulletinCatalog.OrganizationBulletin:
                    {
                        MasterPageFile = "~/Columns/Bulletin.master";
                    }
                    break;
                case BulletinCatalog.SocailBulletin:
                    {
                        MasterPageFile = "~/Forums/Visitor.Master";
                    }
                    break;
            }

            base.OnPreInit(e);

            EventInfo eventInfo = Master.GetType().GetEvent("OnLoadLeftPanels");
            if (eventInfo != null && eventInfo.EventHandlerType == typeof(EventHandler))
            {
                eventInfo.AddEventHandler(Master, new EventHandler(ViewBulletin_OnLoadLeftPanels));
            }
        }

        public int TitleLength
        {
            get;
            set;
        }

        public string LengthVariable
        {
            get;
            set;
        }

        public BulletinCatalog BulletinCatalog
        {
            get { return _catalog; }
        }

        protected string GetBulletinTitle(object bulletin)
        {
            if (bulletin == null || string.IsNullOrEmpty(bulletin.ToString())) return "";

            int len = -1;
            if (!int.TryParse(ExpertMIS.Codes.Utility.GetEnvironmentValue(LengthVariable), out len))
            {
                len = TitleLength;
            }
            if (len <= 0)
            {
                return bulletin.ToString();
            }
            else
            {
                return ExpertMIS.Codes.Utility.CutLongText(bulletin.ToString(), len);
            }
        }

        private void ViewBulletin_OnLoadLeftPanels(object sender, EventArgs e)
        {
            switch (_catalog)
            {
                case BulletinCatalog.OrganizationBulletin:
                    {
                    }
                    break;
                case BulletinCatalog.SocailBulletin:
                    {
                        ucLastedQuestionList lastedQuestionList = LoadControl("~/UserControl/ucLastedQuestionList.ascx") as ucLastedQuestionList;
                        if(lastedQuestionList != null)
                        {
                            PropertyInfo pInfo = Master.GetType().GetProperty("LeftPanel");
                            if (pInfo != null)
                            {
                                Control LeftPanel = pInfo.GetValue(Master, null) as Control;
                                if (LeftPanel != null)
                                {
                                    LeftPanel.Controls.Add(lastedQuestionList);
                                }
                            }
                        }
                    }
                    break;
            }
        }

        protected void repBulletinList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Image imgNew = e.Item.FindControl("imgNew") as Image;
            if (imgNew != null)
            {
                BulletinEntity bulletin = e.Item.DataItem as BulletinEntity;
                if (bulletin != null)
                {
                    // 一周内算新公告，显示imgNew
                    TimeSpan span = DateTime.Now - (DateTime)bulletin.UpdateDate;
                    imgNew.Visible = span.Days <= 7;
                }
            }
        }
    }
}
